﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>过程检验管理</title>
<link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <style>
        .search-container {
            background: #fff;
            padding: 15px;
            margin-bottom: 10px;
            border-radius: 2px;
            box-shadow: 0 1px 2px rgba(0,0,0,.05);
        }
        .search-row {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
            flex-wrap: wrap;
        }
        .search-item {
            display: flex;
            align-items: center;
            margin-right: 30px;
            margin-bottom: 10px;
        }
        .search-label {
            width: 80px;
            text-align: right;
            margin-right: 10px;
            font-size: 14px;
        }
        .search-input {
            width: 180px;
        }
        .btn-container {
            background: #fff;
            padding: 15px;
            margin-bottom: 10px;
            border-radius: 2px;
            box-shadow: 0 1px 2px rgba(0,0,0,.05);
        }
        .table-container {
            background: #fff;
            padding: 0;
            border-radius: 2px;
            box-shadow: 0 1px 2px rgba(0,0,0,.05);
        }
        .layui-table {
            margin: 0;
        }
        .layui-table th {
            background-color: #f8f9fa;
            font-weight: 600;
        }
        .operation-btn {
            margin-right: 5px;
        }
        .status-pending { color: #ff5722; }
        .status-processing { color: #2196f3; }
        .status-completed { color: #4caf50; }
        .status-approved { color: #607d8b; }
    </style>
</head>
<body class="layui-bg-gray">
    <div class="layui-container-fluid" style="padding: 15px;">
        <!-- 搜索条件区域 -->
        <div class="search-container">
            <div class="search-row">
                <div class="search-item">
                    <label class="search-label">检验单编号</label>
                    <input type="text" id="processCode" class="layui-input search-input" placeholder="请输入检验单编号">
                </div>
                <div class="search-item">
                    <label class="search-label">检验类型</label>
                    <select id="processType" class="layui-input search-input">
                        <option value="">请选择检验类型</option>
                        <option value="来料检验">来料检验</option>
                        <option value="过程检验">过程检验</option>
                        <option value="成品检验">成品检验</option>
                        <option value="出货检验">出货检验</option>
                    </select>
                </div>
                <div class="search-item">
                    <label class="search-label">工单编号</label>
                    <input type="text" id="orderCode" class="layui-input search-input" placeholder="请输入工单编号">
                </div>
                <div class="search-item">
                    <label class="search-label">产品物料编码</label>
                    <input type="text" id="productCode" class="layui-input search-input" placeholder="请输入产品物料编码">
                </div>
            </div>
            <div class="search-row">
                <div class="search-item">
                    <label class="search-label">产品物料名称</label>
                    <input type="text" id="productName" class="layui-input search-input" placeholder="请输入产品物料名称">
                </div>
                <div class="search-item">
                    <label class="search-label">检测结果</label>
                    <select id="testingResult" class="layui-input search-input">
                        <option value="">请选择检测结果</option>
                        <option value="检验通过">检验通过</option>
                        <option value="检验不通过">检验不通过</option>
                    </select>
                </div>
                <div class="search-item">
                    <button type="button" class="layui-btn" id="searchBtn">
                        <i class="layui-icon layui-icon-search"></i> 搜索
                    </button>
                    <button type="button" class="layui-btn layui-btn-primary" id="resetBtn">
                        <i class="layui-icon layui-icon-refresh"></i> 重置
                    </button>
                </div>
            </div>
        </div>

        <!-- 操作按钮区域 -->
        <div class="btn-container">
            <button type="button" class="layui-btn layui-btn-sm" id="addBtn">
                <i class="layui-icon layui-icon-add-1"></i> 新增
            </button>
            <button type="button" class="layui-btn layui-btn-sm" id="editBtn">
                <i class="layui-icon layui-icon-edit"></i> 修改
            </button>
            <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" id="deleteBtn">
                <i class="layui-icon layui-icon-delete"></i> 删除
            </button>
        </div>

        <!-- 数据表格 -->
        <div class="table-container">
            <table class="layui-table" lay-size="sm">
			<thead>
				<tr>
                        <th width="50">
                            <input type="checkbox" id="checkAll" lay-skin="primary">
                        </th>
                        <th width="160">检验单编号</th>
                        <th width="100">检验类型</th>
                        <th width="140">工单编号</th>
                        <th width="120">产品物料编码</th>
                        <th width="120">产品物料名称</th>
                        <th width="100">规格型号</th>
                        <th width="80">单位</th>
                        <th width="80">检测数量</th>
                        <th width="100">检测结果</th>
                        <th width="120">检测日期</th>
                        <th width="100">检测人员</th>
                        <th width="100">单据状态</th>
                        <th width="120">操作</th>
				</tr>
			</thead>
                <tbody id="dataTable">
                    <!-- 数据将通过JavaScript动态加载 -->
                </tbody>
		</table>
        </div>

        <!-- 分页区域 -->
        <div id="pagination" style="text-align: right; margin-top: 15px;"></div>
    </div>

    <!-- 添加弹窗模板 -->
    <div id="processDetailDialog" style="display: none; padding: 20px;">
        <form class="layui-form" lay-filter="processDetailForm">
            <div class="layui-form-item">
                <label class="layui-form-label">检验单编号</label>
                <div class="layui-input-block">
                    <input type="text" name="processCode" class="layui-input" readonly>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">检验单名称</label>
                <div class="layui-input-block">
                    <input type="text" name="processName" class="layui-input" readonly>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">检验类型</label>
                <div class="layui-input-block">
                    <select name="processType" lay-filter="processType" lay-verify="required">
                        <option value="">请选择检验类型</option>
                        <option value="过程检验">过程检验</option>
                        <option value="来料检验">来料检验</option>
                        <option value="成品检验">成品检验</option>
                        <option value="出货检验">出货检验</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">工单编号</label>
                <div class="layui-input-block">
                    <input type="text" name="orderCode" class="layui-input" readonly>
                    <button type="button" class="layui-btn layui-btn-primary select-work-order-btn" style="position: absolute; top: 0; right: 0;">
                        <i class="layui-icon layui-icon-search"></i>
                    </button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">工单名称</label>
                <div class="layui-input-block">
                    <input type="text" name="orderName" class="layui-input" readonly>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">工作站编号</label>
                <div class="layui-input-block">
                    <input type="text" name="workStationCode" class="layui-input" readonly>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">工作站名称</label>
                <div class="layui-input-block">
                    <input type="text" name="workStationName" class="layui-input" readonly>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">检测数量</label>
                <div class="layui-input-block">
                    <input type="number" name="testingNum" class="layui-input" readonly>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">更多信息</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-normal" id="showMoreBtn">
                        <i class="layui-icon layui-icon-down"></i> 展开
                    </button>
                </div>
            </div>
            <div id="moreInfo" style="display: none;">
                <div class="layui-form-item">
                    <label class="layui-form-label">检测日期</label>
                    <div class="layui-input-block">
                        <input type="text" name="testingDate" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">检测结果</label>
                    <div class="layui-input-block">
                        <select name="testingResult" lay-filter="testingResult">
                            <option value="">请选择检测结果</option>
                            <option value="检验通过">检验通过</option>
                            <option value="检验不通过">检验不通过</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">检测人员</label>
                    <div class="layui-input-block">
                        <input type="text" name="testingPerson" class="layui-input" readonly>
                    </div>
                </div>
            </div>
        </form>
        
        <!-- 检验项表格 -->
        <div class="layui-form-item" style="margin-top: 20px;">
            <table class="layui-table" lay-size="sm">
                <thead>
                    <tr>
                        <th style="width: 40px;"><input type="checkbox" lay-skin="primary" lay-filter="checkAll"></th>
                        <th>检测项名称</th>
                        <th>检测项类型</th>
                        <th>检测工具</th>
                        <th>检测要求</th>
                        <th>标准值</th>
                        <th>单位</th>
                        <th>误差上限</th>
                        <th>误差下限</th>
                        <th>致命缺陷数量</th>
                    </tr>
                </thead>
                <tbody id="testItemsTable">
                    <!-- 检验项数据将通过JavaScript动态加载 -->
                </tbody>
            </table>
            <div id="testItemsPagination"></div>
        </div>
    </div>

    <!-- 工单选择弹出框 -->
    <div id="workOrderDialog" style="display: none; padding: 20px;">
        <div class="layui-form" lay-filter="workOrderSearchForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">工单编码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="workOrderCode" class="layui-input" placeholder="请输入工单编码">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">工单名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="workOrderName" class="layui-input" placeholder="请输入工单名称">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">产品编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="productCode" class="layui-input" placeholder="请输入产品编号">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">产品名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="productName" class="layui-input" placeholder="请输入产品名称">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">客户名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="customerName" class="layui-input" placeholder="请输入客户名称">
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn" id="searchWorkOrderBtn">
                        <i class="layui-icon layui-icon-search"></i> 搜索
                    </button>
                    <button type="button" class="layui-btn layui-btn-primary" id="resetWorkOrderBtn">
                        <i class="layui-icon layui-icon-refresh"></i> 重置
                    </button>
                </div>
            </div>
        </div>
        <table id="workOrderTable" lay-filter="workOrderTable"></table>
    </div>

<script src="~/scripts/jquery-3.4.1.min.js"></script>
<script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
<script>
        // 全局变量
        var pageConfig = {
            current: 1,
            limit: 10,
            total: 0
        };

        // 页面初始化
        $(document).ready(function() {
            loadProcessData();
            bindEvents();
        });

        // 绑定事件
        function bindEvents() {
            $('#searchBtn').click(function() {
                pageConfig.current = 1;
                loadProcessData();
            });
            
            $('#resetBtn').click(function() {
                resetSearchForm();
                pageConfig.current = 1;
                loadProcessData();
            });
            
            $('#addBtn').click(function() {
                window.location.href = '/Process/AddProces';
            });
            
            $('#editBtn').click(function() {
                var selectedRows = getSelectedRows();
                if (selectedRows.length === 0) {
                    layer.msg('请选择要修改的记录', {icon: 7});
                    return;
                }
                if (selectedRows.length > 1) {
                    layer.msg('只能选择一条记录进行修改', {icon: 7});
                    return;
                }
                editProcess(selectedRows[0]);
            });
            
            $('#deleteBtn').click(function() {
                var selectedRows = getSelectedRows();
                if (selectedRows.length === 0) {
                    layer.msg('请选择要删除的记录', {icon: 7});
                    return;
                }
                deleteProcess(selectedRows);
            });
            
            $('#checkAll').on('change', function() {
                var checked = $(this).prop('checked');
                $('.row-checkbox').prop('checked', checked);
            });
            
            $(document).on('change', '.row-checkbox', function() {
                var allChecked = $('.row-checkbox:not(:checked)').length === 0;
                $('#checkAll').prop('checked', allChecked);
            });

            // 绑定工单选择按钮点击事件
            $(document).on('click', '.select-work-order-btn', function() {
                var inputField = $(this).prev('input');
                showWorkOrderDialog(inputField);
            });
        }

        // 显示消息
        function showMessage(msg, type) {
            layui.use(['layer'], function() {
                var layer = layui.layer;
                var icon = type === 'success' ? 1 : (type === 'warning' ? 7 : 2);
                layer.msg(msg, {icon: icon});
            });
        }

        // 加载数据
        function loadProcessData() {
            var searchParams = getSearchParams();
            
            var loadingIndex = layer.load(2, {
                shade: [0.3, '#000']
            });
            
            $.ajax({
                url: '/Process/GetProces',
                type: 'GET',
                data: $.extend(searchParams, {
                    page: pageConfig.current,
                    limit: pageConfig.limit
                }),
                dataType: 'json',
                success: function(response) {
                    layer.close(loadingIndex);
                    console.log('服务器响应:', response);
                    
                    if (response && response.success && response.data) {
                        displayProcessData(response.data);
                        pageConfig.total = response.total || 0;
                        renderPagination();
                    } else {
                        console.warn('响应数据格式不正确:', response);
                        $('#dataTable').html('<tr><td colspan="14" style="text-align: center; color: #999;">暂无数据</td></tr>');
                        pageConfig.total = 0;
                        renderPagination();
                    }
                },
                error: function(xhr, status, error) {
                    layer.close(loadingIndex);
                    console.error('请求失败:', {
                        status: status,
                        error: error,
                        response: xhr.responseText
                    });
                    showMessage('数据加载失败: ' + error, 'error');
                    $('#dataTable').html('<tr><td colspan="14" style="text-align: center; color: #f56c6c;">数据加载失败</td></tr>');
                }
            });
        }

        // 显示数据
        function displayProcessData(data) {
            if (!data || data.length === 0) {
                $('#dataTable').html('<tr><td colspan="14" style="text-align: center; color: #999;">暂无数据</td></tr>');
                return;
            }
            
            var html = '';
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                var statusClass = getStatusClass(item.state);
                var statusText = getStateText(item.state);
                
                html += '<tr>';
                html += '<td><input type="checkbox" class="row-checkbox" value="' + item.id + '" lay-skin="primary"></td>';
                html += '<td><a href="javascript:;" onclick="showProcessDetail(\'' + item.id + '\')">' + (item.processCode || '') + '</a></td>';
                html += '<td>' + (item.processType || '') + '</td>';
                html += '<td>' + (item.orderCode || '') + '</td>';
                html += '<td>' + (item.productCode || '') + '</td>';
                html += '<td>' + (item.productName || '') + '</td>';
                html += '<td>' + (item.specifications || '') + '</td>';
                html += '<td>' + (item.unitt || '') + '</td>';
                html += '<td>' + (item.testingNum || 0) + '</td>';
                html += '<td>' + (item.testingResult || '') + '</td>';
                html += '<td>' + formatDate(item.testingDate) + '</td>';
                html += '<td>' + (item.testingPerson || '') + '</td>';
                html += '<td><span class="' + statusClass + '">' + statusText + '</span></td>';
                html += '<td>';
                html += '<button class="layui-btn layui-btn-xs operation-btn" onclick="editProcess(' + item.id + ')">修改</button>';
                html += '<button class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteProcess([' + item.id + '])">删除</button>';
                html += '</td>';
                html += '</tr>';
            }
            $('#dataTable').html(html);
            $('#checkAll').prop('checked', false);
        }

        // 渲染分页
        function renderPagination() {
            layui.use(['laypage'], function() {
                var laypage = layui.laypage;
                laypage.render({
                    elem: 'pagination',
                    count: pageConfig.total,
                    limit: pageConfig.limit,
                    curr: pageConfig.current,
                    limits: [10, 20, 50, 100],
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
                    jump: function(obj, first) {
                        pageConfig.current = obj.curr;
                        pageConfig.limit = obj.limit;
                        if (!first) {
                            loadProcessData();
                        }
                    }
                });
            });
        }

        // 获取搜索参数
        function getSearchParams() {
            return {
                processCode: $('#processCode').val().trim(),
                processType: $('#processType').val(),
                orderCode: $('#orderCode').val().trim(),
                productCode: $('#productCode').val().trim(),
                productName: $('#productName').val().trim(),
                testingResult: $('#testingResult').val()
            };
        }

        // 重置搜索表单
        function resetSearchForm() {
            $('#processCode').val('');
            $('#processType').val('');
            $('#orderCode').val('');
            $('#productCode').val('');
            $('#productName').val('');
            $('#testingResult').val('');
        }

        // 获取选中的行
        function getSelectedRows() {
            var selected = [];
            $('.row-checkbox:checked').each(function() {
                selected.push(parseInt($(this).val()));
            });
            console.log('选中的行:', selected);
            return selected;
        }

        // 修改过程检验
        function editProcess(id) {
            window.location.href = '/Process/EditProcess?id=' + id;
        }

        // 删除过程检验
        function deleteProcess(ids) {
            if (!Array.isArray(ids)) {
                ids = [ids];
            }
            
            layui.use(['layer'], function() {
                var layer = layui.layer;
                layer.confirm('确定要删除选中的记录吗？', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    var token = $('input[name="__RequestVerificationToken"]').val();
                    
                    console.log('删除请求数据:', ids);
                    
                    $.ajax({
                        url: '/Process/DeleteProcess',
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify(ids),
                        headers: {
                            'RequestVerificationToken': token
                        },
                        success: function(response) {
                            console.log('删除响应:', response);
                            if (response.success) {
                                layer.msg('删除成功', {icon: 1}, function() {
                                    pageConfig.current = 1;
                                    loadProcessData();
                                });
                            } else {
                                layer.msg(response.message || '删除失败', {icon: 2});
                            }
                            layer.close(index);
                        },
                        error: function(xhr, status, error) {
                            console.error('删除请求失败:', {
                                status: status,
                                error: error,
                                response: xhr.responseText
                            });
                            layer.msg('删除失败: ' + error, {icon: 2});
                            layer.close(index);
                        }
                    });
                });
            });
        }

        // 格式化日期
        function formatDate(dateString) {
            if (!dateString) return '';
            var date = new Date(dateString);
            if (isNaN(date.getTime())) return '';
            
            var year = date.getFullYear();
            var month = ('0' + (date.getMonth() + 1)).slice(-2);
            var day = ('0' + date.getDate()).slice(-2);
            return year + '-' + month + '-' + day;
        }

        // 获取状态文本
        function getStateText(state) {
            switch(state) {
                case 0: return '待检验';
                case 1: return '检验中';
                case 2: return '检验完成';
                case 3: return '已审核';
                default: return '未知状态';
            }
        }

        // 获取状态样式类
        function getStatusClass(state) {
            switch(state) {
                case 0: return 'status-pending';
                case 1: return 'status-processing';
                case 2: return 'status-completed';
                case 3: return 'status-approved';
                default: return '';
            }
        }

        // 修改显示详情的函数
        function showProcessDetail(id) {
            layui.use(['layer', 'form'], function() {
                var layer = layui.layer;
                var form = layui.form;
                
                var loadingIndex = layer.load(2, {
                    shade: [0.3, '#000']
                });
                
                // 获取详细信息
                $.ajax({
                    url: '/Process/GetProcessById?id=' + id,
                    type: 'GET',
                    success: function(response) {
                        layer.close(loadingIndex);
                        console.log('完整的响应数据:', response);
                        
                        if (response && response.success && response.data) {
                            var data = response.data;
                            console.log('处理的数据:', data);
                            
                            try {
                                // 填充表单数据
                                form.val('processDetailForm', {
                                    'processCode': data.processCode || '',
                                    'processName': data.productName || '',
                                    'processType': data.processType || '',
                                    'orderCode': data.orderCode || '',
                                    'orderName': data.productName || '',
                                    'workStationCode': data.workStationCode || '',
                                    'workStationName': data.workStationName || '',
                                    'testingNum': data.testingNum || 0,
                                    'testingDate': formatDateTime(data.testingDate) || '',
                                    'testingResult': data.testingResult || '',
                                    'testingPerson': data.testingPerson || ''
                                });
                                
                                // 显示弹窗
                                layer.open({
                                    type: 1,
                                    title: '查看检验单信息',
                                    area: ['800px', '600px'],
                                    content: $('#processDetailDialog'),
                                    success: function() {
                                        // 重新渲染表单元素
                                        form.render();
                                        // 默认展开更多信息
                                        $('#moreInfo').show();
                                        $('#showMoreBtn').html('<i class="layui-icon layui-icon-up"></i> 收起');
                                    }
                                });
                            } catch (err) {
                                console.error('处理数据时出错:', err);
                                layer.msg('处理数据时出错: ' + err.message, {icon: 2});
                            }
                        } else {
                            console.error('响应数据格式不正确:', response);
                            layer.msg(response.message || '获取数据失败', {icon: 2});
                        }
                    },
                    error: function(xhr, status, error) {
                        layer.close(loadingIndex);
                        console.error('请求失败:', {
                            status: status,
                            error: error,
                            response: xhr.responseText
                        });
                        layer.msg('获取数据失败: ' + error, {icon: 2});
                    }
                });
            });
        }

        // 添加更多信息展开/收起功能
        $(document).on('click', '#showMoreBtn', function() {
            var $moreInfo = $('#moreInfo');
            var $icon = $(this).find('.layui-icon');
            if ($moreInfo.is(':visible')) {
                $moreInfo.slideUp();
                $icon.removeClass('layui-icon-up').addClass('layui-icon-down');
                $(this).html('<i class="layui-icon layui-icon-down"></i> 展开');
            } else {
                $moreInfo.slideDown();
                $icon.removeClass('layui-icon-down').addClass('layui-icon-up');
                $(this).html('<i class="layui-icon layui-icon-up"></i> 收起');
            }
        });

        // 显示工单选择弹出框
        function showWorkOrderDialog(inputField) {
            layui.use(['layer', 'table'], function() {
                var layer = layui.layer;
                var table = layui.table;
                
                console.log('开始显示工单选择弹出框');
                
                // 打开弹出框
                var layerIndex = layer.open({
                    type: 1,
                    title: '工单选择',
                    area: ['1200px', '600px'],
                    content: $('#workOrderDialog'),
                    success: function(layero, index) {
                        console.log('弹出框打开成功，开始初始化表格');
                        
                        // 初始化表格
                        var tableIns = table.render({
                            elem: '#workOrderTable',
                            url: '/Process/GetProductionWorkOrderList',
                            method: 'get',
                            page: true,
                            limit: 10,
                            cols: [[
                                {type: 'radio', width: 50},
                                {field: 'workOrderCode', title: '工单编码', width: 160},
                                {field: 'workOrderName', title: '工单名称', width: 200},
                                {field: 'productCode', title: '产品编号', width: 120},
                                {field: 'productName', title: '产品名称', width: 200},
                                {field: 'productModel', title: '规格型号', width: 120},
                                {field: 'unit', title: '单位', width: 80},
                                {field: 'workOrderQuantity', title: '工单数量', width: 100},
                                {field: 'customerName', title: '客户名称', width: 160}
                            ]],
                            parseData: function(res) {
                                console.log('原始响应数据:', res);
                                
                                // 检查响应数据格式
                                if (res && Array.isArray(res)) {
                                    console.log('数据是数组格式，长度:', res.length);
                                    return {
                                        "code": 0,
                                        "msg": "",
                                        "count": res.length,
                                        "data": res
                                    };
                                } else if (res && res.data && Array.isArray(res.data)) {
                                    console.log('数据在data属性中，长度:', res.data.length);
                                    return {
                                        "code": 0,
                                        "msg": "",
                                        "count": res.count || res.data.length,
                                        "data": res.data
                                    };
                                } else {
                                    console.error('未知的数据格式:', res);
                                    return {
                                        "code": 1,
                                        "msg": "数据格式错误",
                                        "count": 0,
                                        "data": []
                                    };
                                }
                            },
                            done: function(res, curr, count) {
                                console.log('表格渲染完成:', res, '当前页:', curr, '总数:', count);
                            }
                        });

                        // 监听行选择事件
                        table.on('radio(workOrderTable)', function(obj) {
                            console.log('选中工单:', obj.data);
                            var data = obj.data;
                            
                            // 填充表单数据
                            $('input[name="orderCode"]').val(data.workOrderCode || '');
                            $('input[name="orderName"]').val(data.workOrderName || '');
                            
                            // 关闭弹出框
                            layer.close(layerIndex);
                            
                            // 显示成功消息
                            layer.msg('工单选择成功', {icon: 1});
                        });

                        // 搜索按钮事件
                        $('#searchWorkOrderBtn').off('click').on('click', function() {
                            console.log('执行搜索');
                            var searchData = {
                                workOrderCode: $('input[name="workOrderCode"]').val(),
                                workOrderName: $('input[name="workOrderName"]').val(),
                                productCode: $('input[name="productCode"]').val(),
                                productName: $('input[name="productName"]').val(),
                                customerName: $('input[name="customerName"]').val()
                            };
                            console.log('搜索条件:', searchData);
                            
                            // 重新加载表格
                            table.reload('workOrderTable', {
                                where: searchData,
                                page: {
                                    curr: 1
                                }
                            });
                        });

                        // 重置按钮事件
                        $('#resetWorkOrderBtn').off('click').on('click', function() {
                            console.log('重置搜索条件');
                            $('#workOrderDialog input[type="text"]').val('');
                            
                            // 重新加载表格
                            table.reload('workOrderTable', {
                                where: {},
                                page: {
                                    curr: 1
                                }
                            });
                        });
                    }
                });
            });
        }

        // 格式化日期时间函数
        function formatDateTime(dateString) {
            if (!dateString) return '';
            var date = new Date(dateString);
            if (isNaN(date.getTime())) return '';
            
            var year = date.getFullYear();
            var month = ('0' + (date.getMonth() + 1)).slice(-2);
            var day = ('0' + date.getDate()).slice(-2);
            var hours = ('0' + date.getHours()).slice(-2);
            var minutes = ('0' + date.getMinutes()).slice(-2);
            var seconds = ('0' + date.getSeconds()).slice(-2);
            return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
        }
</script>
</body>
</html>